package com.pmdesigns.sqlmapper;



/**
 * This class contains some shared utility methods
 *
 * @author mike dooley
 */
public class Utils {

	public static String template2GeneratorName(String tname) {
		int idx = tname.indexOf('.');
		if (idx > 0) {
			tname = tname.substring(0, idx);
		}
		return capitalize(tname)+"Generator";
	}

	public static String capitalize(String s) {
		if (s == null || s.length() == 0) return s;
		return Character.toUpperCase(s.charAt(0)) + s.substring(1);
	}

	public static String camel(String s) {
		if (s == null || s.length() == 0 || s.indexOf('_') == -1) return s;
		StringBuilder sb = new StringBuilder(s);
		int n = s.length();
		for (int i=0; i < n; i++) {
			if (sb.charAt(i) == '_') {
				sb.deleteCharAt(i);
				n--;
				if (i < n) {
					char c = sb.charAt(i);
					if (Character.isLowerCase(c)) {
						sb.setCharAt(i, Character.toUpperCase(c));
					}
				}
			}
		}
		return sb.toString();
	}

	public static String singularize(String s) {
		// simplest possible algorithm...chop trailing 's' or 'S', if it exists
		int n;
		if (s == null || (n=s.length()) == 0) return s;
		char c = s.charAt(n-1);
		return (c == 's' || c == 'S') ? s.substring(0,n-1) : s;
	}
}
